package com.ftg.learn.framework.mybatis.chapter02;

import com.ftg.learn.framework.mybatis.chapter01.DemoMapper;
import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

/**
 * @author 布衣
 */
public class JavaBase {

    public static void main(String[] args) {
        SqlSession sqlSession = null;

        SqlSessionFactory sqlSessionFactory = null;

        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

//        配置文件
        Configuration c = new Configuration();
        MysqlDataSource d = new MysqlDataSource();
        d.setUser("root");
        d.setPassword("123456");
        d.setURL("jdbc:mysql://localhost:3306/dbnd");
        Environment env = new Environment("dev", new JdbcTransactionFactory(), d);
        c.setEnvironment(env);

        c.addMapper(DemoMapper.class);

        //settings
        c.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);

        sqlSessionFactory = sqlSessionFactoryBuilder.build(c);

        sqlSession = sqlSessionFactory.openSession();

        sqlSession.getMapper(DemoMapper.class).list();
    }


}
